package teleradiologia.web;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lowagie.text.DocumentException;

import teleradiologia.domain.Doctor;
import teleradiologia.domain.Managerdiagnosticlab;
import teleradiologia.domain.Offer;
import teleradiologia.domain.Person;
import teleradiologia.service.DiagnosticlabManipulator;
import teleradiologia.service.DoctorManipulator;
import teleradiologia.service.ManagerdiagnosticlabManipulator;
import teleradiologia.service.OfferManipulator;
import teleradiologia.service.OrdertypeManipulator;
import teleradiologia.service.PersonManipulator;
import teleradiologia.utils.ReportsCreator;

@Controller
public class DiagnosticManagerController {
	private static final Log log = LogFactory.getLog(AdminController.class);

	private DoctorManipulator doctorManipulator;

	private ManagerdiagnosticlabManipulator managerDiagnosticlabManipulator;
	private PersonManipulator personManipulator;
	private OfferManipulator offerManipulator;
	private OrdertypeManipulator ordertypeManipulator;
	private DiagnosticlabManipulator diagnosticlabManipulator;

	public void setManagerDiagnosticlabManipulator(
			ManagerdiagnosticlabManipulator managerDiagnosticlabManipulator) {
		this.managerDiagnosticlabManipulator = managerDiagnosticlabManipulator;
	}

	public void setPersonManipulator(PersonManipulator personManipulator) {
		this.personManipulator = personManipulator;
	}

	public void setOfferManipulator(OfferManipulator offerManipulator) {
		this.offerManipulator = offerManipulator;
	}

	public void setDoctorManipulator(DoctorManipulator doctorManipulator) {
		this.doctorManipulator = doctorManipulator;
	}

	public DoctorManipulator getDoctorManipulator() {
		return doctorManipulator;
	}

	public DiagnosticlabManipulator getDiagnosticlabManipulator() {
		return diagnosticlabManipulator;
	}

	public ManagerdiagnosticlabManipulator getManagerDiagnosticlabManipulator() {
		return managerDiagnosticlabManipulator;
	}

	public void setDiagnosticlabManipulator(
			DiagnosticlabManipulator diagnosticlabManipulator) {
		this.diagnosticlabManipulator = diagnosticlabManipulator;
	}

	public void setOrdertypeManipulator(
			OrdertypeManipulator ordertypeManipulator) {
		this.ordertypeManipulator = ordertypeManipulator;
	}

	@RequestMapping("/diag_manager/managerHome.htm")
	public ModelAndView handleManagerHome() {
		return new ModelAndView("diag_manager/managerHome");
	}

	@RequestMapping("/diag_manager/manipulateOffers.htm")
	public ModelAndView handleManipulateOffers() {

		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		Managerdiagnosticlab manager = this.managerDiagnosticlabManipulator
				.getByIdPerson(me.getId());

		String krs = manager.getDiagnosticlab().getKrsnumber();

		List<Offer> offers = this.offerManipulator
				.getAllByManagerdiagnosticlab(krs);

		myModel.put("offers", offers);
		myModel.put("krs", krs);

		ModelAndView result = new ModelAndView("diag_manager/manipulateOffers",
				"model", myModel);

		return result;
	}

	@RequestMapping("/diag_manager/addOfferHome.htm")
	public ModelAndView handleAddOfferHome(@RequestParam("krs") String krs) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("ordertypes", this.ordertypeManipulator.getAll());
		myModel.put("krs", krs);

		ModelAndView result = new ModelAndView("diag_manager/addOfferHome",
				"model", myModel);

		return result;
	}

	@RequestMapping("/diag_manager/addOffer.htm")
	public ModelAndView handleAddOffer(@RequestParam("krs") String krs,
			@RequestParam("ordertype") long ordertype,
			@RequestParam("price") String price,
			@RequestParam("time") String time,
			@RequestParam("begin") String begin, @RequestParam("end") String end)
			throws ParseException {

		Offer offer = new Offer();
		offer.setDiagnosticlab(this.diagnosticlabManipulator
				.getByKrsnumber(krs));

		DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
		offer.setBegin(df.parse(begin));
		offer.setEnd(df.parse(end));
		offer.setOrdertype(this.ordertypeManipulator.getById(ordertype));
		offer.setPrice(Long.parseLong(price));
		offer.setTime(time);

		this.offerManipulator.create(offer);

		return handleManipulateOffers();

	}

	@RequestMapping("/diag_manager/editOffers.htm")
	public ModelAndView handleEditOffers() {

		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		Managerdiagnosticlab manager = this.managerDiagnosticlabManipulator
				.getByIdPerson(me.getId());

		String krs = manager.getDiagnosticlab().getKrsnumber();

		List<Offer> offers = this.offerManipulator
				.getAllByManagerdiagnosticlab(krs);

		myModel.put("offers", offers);
		myModel.put("krs", krs);

		ModelAndView result = new ModelAndView("diag_manager/editOffers",
				"model", myModel);

		return result;
	}

	@RequestMapping("/diag_manager/deleteOffer.htm")
	public ModelAndView handleDeleteOffer(@RequestParam("offerId") long offerId) {

		Offer offer = this.offerManipulator.getById(offerId);

		this.offerManipulator.delete(offer);

		ModelAndView result = handleManipulateOffers();

		return result;
	}

	@RequestMapping("/diag_manager/offersReport.htm")
	public ModelAndView handleOffersReport(HttpServletRequest request,
			HttpServletResponse response) {

		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());

		Managerdiagnosticlab manager = this.managerDiagnosticlabManipulator
				.getByIdPerson(me.getId());

		String krs = manager.getDiagnosticlab().getKrsnumber();

		List<Offer> offers = this.offerManipulator
				.getAllByManagerdiagnosticlab(krs);

		response.setHeader("Content-Disposition",
				"attachment; filename=\"offersReport.pdf\"");

		try {
			ReportsCreator.generateOfferReport(response.getOutputStream(),
					manager.getDiagnosticlab(), offers);

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@RequestMapping("/diag_manager/doctorsReport.htm")
	public ModelAndView handleDoctorsReport(HttpServletRequest request,
			HttpServletResponse response) {

		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());

		Managerdiagnosticlab manager = this.managerDiagnosticlabManipulator
				.getByIdPerson(me.getId());

		String krs = manager.getDiagnosticlab().getKrsnumber();

		List<Doctor> doctors = doctorManipulator.getAllByKrsnumber(krs);

		response.setHeader("Content-Disposition","attachment; filename=\"doctorsReport.pdf\"");

		try {
			ReportsCreator.generateDoctorsReport(response.getOutputStream(), manager.getDiagnosticlab(), doctors);

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
}
